#region Directives
using System;
using System.IO;
using System.Web.Services;
using System.Xml;
using System.Xml.Serialization;
#endregion

namespace TeamFoundationServerSensor {

    /// <summary>
    /// Base Class that contains common methods used by all the Endpoint Services.
    /// </summary>
    public abstract class EndpointBase : WebService {
        /// <summary>
        /// Deserializes a Type
        /// </summary>
        /// <typeparam name="T">Type to deserialize</typeparam>
        /// <param name="serializedType">serialized version of the type</param>
        /// <returns>DeSerialized version of the type</returns>
        protected T CreateInstance<T>(string serializedType) where T : new() {
            T customType = new T();

            XmlSerializer serializer = new XmlSerializer(typeof(T));
            StringReader stream = new StringReader(serializedType);
            XmlReader xmlReader = new XmlTextReader(stream);

            try {
                customType = (T)serializer.Deserialize(xmlReader);
            }
            catch {
                //TODO: Add Logging etc here...
                throw;
            }

            return customType;
        }

        /// <summary>
        /// Endpoint required to accept incoming <see href="BuildCompletionEvent"/> notifications
        /// </summary>
        /// <param name="eventXml">Serialized version of the <see href="BuildCompletionEvent"/></param>
        /// <param name="tfsIdentityXml">Serialized version of <see href="TFSIdentity"/></param>
        /// <returns>True if method succeeded in reading data. False otherwise.</returns>
        public abstract Boolean Notify(string eventXml, string tfsIdentityXml);
    }
}